package com.example.bailin.giftsay.main.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.entity.SelectGiftEntity;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by bailin on 16/8/25.
 */
public class ShowAllAdapter extends RecyclerView.Adapter<ShowAllAdapter.ShowAllViewHolder> {
    private SelectGiftEntity entity;
    private int index;

    public void setIndex(int index) {
        this.index = index;
        notifyDataSetChanged();
    }

    public void setEntity(SelectGiftEntity entity) {
        this.entity = entity;
        notifyDataSetChanged();
    }

    @Override
    public ShowAllViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(MyApp.getContext()).inflate(R.layout.item_hot, parent, false);
        ShowAllViewHolder viewHolder = new ShowAllViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ShowAllViewHolder holder, int position) {
        List<SelectGiftEntity.DataBean.ItemsBean> items = new ArrayList<>();
        for (int i = 0; i < entity.getData().getItems().size(); i++) {
            items.add(entity.getData().getItems().get(i));
        }

        NetTool tool = new NetTool();
        switch (index) {
            case 0:
                tool.getImg(entity.getData().getItems().get(position).getCover_image_url(), holder.ivHotCoverImageUrl);
                holder.tvHotName.setText(entity.getData().getItems().get(position).getName());
                holder.tvHotPrice.setText("¥" + entity.getData().getItems().get(position).getPrice());
                holder.tvHotCount.setText("" + entity.getData().getItems().get(position).getFavorites_count());
                break;

            case 1:
                Collections.sort(items, new FavoritesCount());
                tool.getImg(items.get(position).getCover_image_url(), holder.ivHotCoverImageUrl);
                holder.tvHotName.setText(items.get(position).getName());
                holder.tvHotPrice.setText("¥" + items.get(position).getPrice());
                holder.tvHotCount.setText("" + items.get(position).getFavorites_count());
                break;

            case 2:
                Collections.sort(items, new Price());
                tool.getImg(items.get(position).getCover_image_url(), holder.ivHotCoverImageUrl);
                holder.tvHotName.setText(items.get(position).getName());
                holder.tvHotPrice.setText("¥" + items.get(position).getPrice());
                holder.tvHotCount.setText("" + items.get(position).getFavorites_count());
                break;

            case 3:
                Collections.sort(items, new Price2());
                tool.getImg(items.get(position).getCover_image_url(), holder.ivHotCoverImageUrl);
                holder.tvHotName.setText(items.get(position).getName());
                holder.tvHotPrice.setText("¥" + items.get(position).getPrice());
                holder.tvHotCount.setText("" + items.get(position).getFavorites_count());
                break;
        }
    }

    @Override
    public int getItemCount() {
        return entity.getData().getItems().size();
    }

    public class ShowAllViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivHotCoverImageUrl; // 大图片
        private TextView tvHotName; // 简单介绍
        private TextView tvHotPrice;// 价格
        private TextView tvHotCount; // 收藏次数
        private RelativeLayout rlHot;

        public ShowAllViewHolder(View itemView) {
            super(itemView);
            ivHotCoverImageUrl = (ImageView) itemView.findViewById(R.id.iv_hot_cover_image_url);
            tvHotName = (TextView) itemView.findViewById(R.id.tv_hot_name);
            tvHotPrice = (TextView) itemView.findViewById(R.id.tv_hot_price);
            tvHotCount = (TextView) itemView.findViewById(R.id.tv_hot_count);
            rlHot = (RelativeLayout) itemView.findViewById(R.id.rl_hot_id);
        }
    }

    public class FavoritesCount implements Comparator {
        @Override
        public int compare(Object lhs, Object rhs) {
            SelectGiftEntity.DataBean.ItemsBean p1 = (SelectGiftEntity.DataBean.ItemsBean) lhs;
            SelectGiftEntity.DataBean.ItemsBean p2 = (SelectGiftEntity.DataBean.ItemsBean) rhs;
            return new Integer(p2.getFavorites_count()).compareTo(new Integer(p1.getFavorites_count()));
        }
    }

    public class Price implements Comparator {
        @Override
        public int compare(Object lhs, Object rhs) {
            SelectGiftEntity.DataBean.ItemsBean p1 = (SelectGiftEntity.DataBean.ItemsBean) lhs;
            SelectGiftEntity.DataBean.ItemsBean p2 = (SelectGiftEntity.DataBean.ItemsBean) rhs;
            return new Double(p1.getPrice()).compareTo(new Double(p2.getPrice()));
        }
    }

    public class Price2 implements Comparator {
        @Override
        public int compare(Object lhs, Object rhs) {
            SelectGiftEntity.DataBean.ItemsBean p1 = (SelectGiftEntity.DataBean.ItemsBean) lhs;
            SelectGiftEntity.DataBean.ItemsBean p2 = (SelectGiftEntity.DataBean.ItemsBean) rhs;
            return new Double(p2.getPrice()).compareTo(new Double(p1.getPrice()));
        }
    }
}